import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { UserService } from '@nx-chat/common/service';

@Component({
  selector: 'user-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss']
})
export class UserSearchComponent {

  /**输入框绑定的用户 */
  public inputValue;

  /**过滤后的用户 */
  public filteredUsers = [];

  /**好友列表 */
  list = [];

  constructor(
    private _router: Router,
    private _activateRoute: ActivatedRoute,
    private _userService: UserService,
  ) {
    const uid = this._userService.uId;
    this._userService.getFriendsList(uid).subscribe(data => {
      this.list = data;
    })
  }

  public async filterUsers(event) {
    const filtered : any[] = [];
    const query = event.query;
    for(let i = 0; i < this.list.length; i++) {
      const user = this.list[i];
      if (user.name.toLowerCase().indexOf(query.toLowerCase()) == 0) {
        filtered.push(user);
      }
    }

    this.filteredUsers = filtered;
  }

  public getChat(uid: string) {
    this.inputValue = null;
    this._router.navigate([`message/${uid}`]);
  }

}
